#!/bin/bash
set -e  # 当任何命令失败时，自动退出脚本

work_dir="/media/adt/T7/zwh/work_space"
rosbag_dir="$work_dir/bags"
data_path="$work_dir/datasets"
out_path="$work_dir/dataview"
anno_path="$work_dir/annotate"


# 封装命令执行和错误处理的函数
run_command() {
    local cmd="$1"
    local dsc="$2"
    echo "Running: $cmd"
    eval $cmd
    if [ $? -ne 0 ]; then
        echo "$dsc 运行失败，停止执行！！！"
        exit 1
    else
      echo "$dsc 运行成功。"
    fi
}


#####
# 添加脚本规则:
# run_command "python 脚本 参数列表"  "功能描述”
#####

echo "run_carla.sh 开始处理 $data_path"

# 解析 rosbag   sh脚本运行可能有问题，则手动运行即可
cd carla2real || { echo "无法进入 carla2real 目录"; exit 1; }

run_command "python carla2real_before.py --data-path '$data_path'" "carla2real前处理"

run_command "python label_map_preprocess.py --data-path '$data_path'" "语义图片处理"

run_command "python gbuffers_preprocess.py --data-path '$data_path'" "gbuffers图片处理"

run_command "python generate_fake_txt.py --data-path '$data_path'" "生成fake_txt"

cd carla2real_engine/code || { echo "无法进入 carla2real_engine/code 目录"; exit 1; }
export PYTHONPATH="${PYTHONPATH}:."
run_command "python epe/EPEExperiment.py test config/test_pfd2cs.yaml --log info --data-path '$data_path'" "carla2real数据增强"
cd ../.. || { echo "无法退出 carla2real_engine/code 目录"; exit 1; }

run_command "python carla2real_after.py --data-path '$data_path'" "carla2real后处理"

cd .. || { echo "无法退出 carla2real 目录"; exit 1; }

echo "处理完毕"